package com.cardbaobao.cardbabyclient.utils;

import android.util.Base64;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.Key;

import javax.crypto.Cipher;

/**
 * Created by Jiangzx on 2017/5/11.
 * RSA加密解密工具类
 */

public class RsaUtils {
    /**RSA最大加密明文大小*/
    private static final int MAX_ENCRYPT_BLOCK = 117;
    /**RSA最大解密密文大小*/
    private static final int MAX_DECRYPT_BLOCK = 128;
    /**密码系统类型*/
    private static final String CIPHER_TYPE="RSA/ECB/PKCS1Padding";

    /**
     * RSA解密
     * @param inputStr 需要解密的字符串
     * @param privateKey rsa解密用的key
     * @return 解密后的字符串，如果发生异常，返回空字符串
     */
    public static String decryptByPrivateKey(String inputStr, Key privateKey){
        ByteArrayOutputStream out = null;
        String result="";
        try{
            Cipher cipher=Cipher.getInstance(CIPHER_TYPE);
            cipher.init(Cipher.DECRYPT_MODE,privateKey);
            byte[] inputBytes= Base64.decode(inputStr,Base64.DEFAULT);
            int inputLength=inputBytes.length;
            out=new ByteArrayOutputStream();
            int offset=0;
            byte[] cache;
            int i=0;
            //对数据分段解密
            for(;inputLength-offset>0;){
                if(inputLength-offset>MAX_DECRYPT_BLOCK){
                    cache=cipher.doFinal(inputBytes,offset,MAX_DECRYPT_BLOCK);
                }else{
                    cache=cipher.doFinal(inputBytes,offset,inputLength-offset);
                }
                out.write(cache,0,cache.length);
                i++;
                offset=i*MAX_DECRYPT_BLOCK;
            }
            result=new String(out.toByteArray());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     *RSA加密
     * @param inputStr 需要加密的字符串
     * @param publicKey rsa加密用的key
     * @return 加密后的字符串
     */
    public static String encryptByPublicKey(String inputStr,Key publicKey){
        ByteArrayOutputStream out=null;
        String result="";
        try{
            Cipher cipher=Cipher.getInstance(CIPHER_TYPE);
            cipher.init(Cipher.ENCRYPT_MODE,publicKey);
            byte[] inputBytes=inputStr.getBytes();
            int inputLength=inputBytes.length;
            out=new ByteArrayOutputStream();
            int offset=0;
            int i=0;
            byte[] cache;
            //对数据分段加密
            for(;inputLength-offset>0;){
                if(inputLength-offset>MAX_ENCRYPT_BLOCK){
                    cache=cipher.doFinal(inputBytes,offset,MAX_ENCRYPT_BLOCK);
                }else{
                    cache=cipher.doFinal(inputBytes,offset,inputLength-offset);
                }
                out.write(cache,0,cache.length);
                i++;
                offset=i*MAX_ENCRYPT_BLOCK;
            }
            result=Base64.encodeToString(out.toByteArray(),Base64.DEFAULT);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}
